package com.qcu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcu.dto.RegisterDto;
import com.qcu.entity.User;
import com.qcu.mapper.UserMapper;
import com.qcu.service.UserService;
import com.qcu.util.PasswordUtils;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


/**
* @author MyComputer
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-02-06 13:07:07
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Resource
    private UserMapper userMapper;


    /**
     * 保存用户信息
     *
     * @param
     * @return
     */
    @Override
    public Response saveUser(RegisterDto user1) {
        User user = new User();
        BeanUtils.copyProperties(user1, user);

        String salt = PasswordUtils.generateSalt();
        String hashPassword = PasswordUtils.hashPassword(user.getPasswordHash(), salt);
        user.setSalt(salt);
        user.setPasswordHash(hashPassword);
        int insert = userMapper.insert(user);
        return insert > 0 ? Response.success() : Response.failure(ResponseCode.FAILURE, "添加失败");
    }

}




